package com.zhongxin.wymovieticketfragment;

import java.util.ArrayList;




import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.support.v4.app.Fragment;
import android.support.v4.util.LruCache;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxin.wymovieticket.MovieGuideActivity;
import com.zhongxin.wymovieticket.R;
import com.zhongxin.wymovieticket.SubPriceActivity;
import com.zhoujing.Entity.ActityHotEntity;
import com.zhoujing.Entity.ActivityEntity;
import com.zhoujing.Entity.ActivitySpannerContentEntity;
import com.zhoujing.adapter.ActivityListitemAdapter;
import com.zhoujing.adapter.HeadViewpagerAdapter;
import com.zhoujing.utils.ImageDownLoadUtils;
import com.zhoujing.utils.ImageDownload_NetworkImageView;
import com.zhoujing.utils.JsonUtils;
import com.zhoujing.utils.JsonUtils.JsonObjectCallBack;

public class ActivityFragment extends Fragment implements android.view.View.OnClickListener{

	// 暂定的数据
	private final static String PATH = "http://piao.163.com/m/promotion/tab.html?data=fPwcnIS5OgcO9mBbudhgglOP6LqTR2XUAp1aihzgjLAZzL3qqEQKWw%3D%3D&stamp=1411721068094&apiVer=14&mobileType=android&deviceId=08:00:27:f4:e2:00&channel=netease&ver=4.8&city=440300";
	// 声明请求队列
	private RequestQueue queue;

	//
	private PullToRefreshListView refreshListView;
	private ActivityListitemAdapter activityListitemAdapter;
	private List<ActityHotEntity> list=new ArrayList<ActityHotEntity>();
	private ListView listView;
	//头部的viewpager
	private ImageView headiImageView;
	private ViewPager headviewPager;
	//viewpager 里添加的view
	private List<View> views;
	private Button activityPriceButton,activityGuideButton;
	public ActivityFragment() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		queue = Volley.newRequestQueue(getActivity());
		//声明缓存
		LruCache<String, Bitmap> lruCache=ImageDownload_NetworkImageView.getLruCache(getActivity());
		activityListitemAdapter = new ActivityListitemAdapter(
				getActivity(), list, queue,lruCache);
		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_activity, container,
				false);
		
		initView(view,inflater);
		getData();
		// 联网取数据
		return view;
	}

	private void initView(View view,LayoutInflater inflater) {
	
		refreshListView = (PullToRefreshListView) view
				.findViewById(R.id.actity_pulltorefreshlisview);
		listView = refreshListView.getRefreshableView();
		View headView=inflater.inflate(R.layout.activity_activity_listview_header, listView, false);
		//添加头部
		//headiImageView=(ImageView) headView.findViewById(R.id.activity_spanner_imageview);
		headviewPager=(ViewPager) headView.findViewById(R.id.activity_spanner_viewpager);
		views=new ArrayList<View>();
		ImageView imageView1=new ImageView(getActivity());
		ImageView imageView2=new ImageView(getActivity());
		imageView1.setImageResource(R.drawable.activity_default);
		imageView2.setImageResource(R.drawable.activity_default);
		views.add(imageView1);
		views.add(imageView2);
		HeadViewpagerAdapter headViewpagerAdapter=new HeadViewpagerAdapter(views);
		headviewPager.setAdapter(headViewpagerAdapter);
		activityPriceButton=(Button) headView.findViewById(R.id.activity_price);
		activityGuideButton=(Button) headView.findViewById(R.id.activity_guide);
		activityPriceButton.setOnClickListener(this);
		activityGuideButton.setOnClickListener(this);
		listView.addHeaderView(headView);
		listView.setAdapter(activityListitemAdapter);
		
	}
	private void getData()
	{
		JsonUtils.parseJsonObject(getActivity(), PATH,
				new JsonObjectCallBack() {

					@Override
					public void jsonObjectCallBack(JSONObject jsonObject) {
						// 解析数据
						try {
							ActivityEntity activityEntity = new ActivityEntity();
							int retcode = jsonObject.getInt("retcode");
							String retdesc = jsonObject.getString("retdesc");
							JSONArray bannerList = jsonObject
									.getJSONArray("bannerList");
							List<ActivitySpannerContentEntity> spannerContentEntities = new ArrayList<ActivitySpannerContentEntity>();
							for (int i = 0; i < bannerList.length(); i++) {

								ActivitySpannerContentEntity entity = new ActivitySpannerContentEntity();
								JSONObject banner = bannerList.getJSONObject(i);
								String title = banner.getString("title");
								String picUrl = banner.getString("picUrl");
								int activityStatus = banner
										.getInt("activityStatus");
								String clickUrl = banner.getString("clickUrl");
								entity.setTitle(title);
								entity.setActivityStatus(activityStatus);
								entity.setClickUrl(clickUrl);
								entity.setPicUrl(picUrl);
								spannerContentEntities.add(entity);

							}
							JSONArray hotList = jsonObject
									.getJSONArray("hotList");
							List<ActityHotEntity> hotEntities = new ArrayList<ActityHotEntity>();
							for (int i = 0; i < hotList.length(); i++) {
								ActityHotEntity entity = new ActityHotEntity();
								JSONObject hot = hotList.getJSONObject(i);
								String title = hot.getString("title");
								String picUrl = hot.getString("picUrl");
								int activityStatus = hot
										.getInt("activityStatus");
								String clickUrl = hot.getString("clickUrl");
								int type = hot.getInt("type");
								entity.setActivityStatus(activityStatus);
								entity.setClickUrl(clickUrl);
								entity.setPicUrl(picUrl);
								entity.setTitle(title);
								entity.setType(type);
								hotEntities.add(entity);
							}
							activityEntity.setRetcode(retcode);
							activityEntity.setRetdesc(retdesc);
							activityEntity
									.setBannerList(spannerContentEntities);
							activityEntity.setHotList(hotEntities);
							if (list!=null&&list.size()>0) {
								list.clear();
							}
								list.addAll(activityEntity.getHotList());
							
							
							activityListitemAdapter.notifyDataSetChanged();
							
							
							//
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}, queue);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.activity_price:
			Intent intent=new Intent(getActivity(),SubPriceActivity.class);
			startActivity(intent);
			break;
        case R.id.activity_guide:
			Intent intent2=new Intent(getActivity(),MovieGuideActivity.class);
			startActivity(intent2);
			break;

		default:
			break;
		}
	}
}
